package chapter.fifth.item28;

import java.util.Iterator;
import java.util.List;

public class BoundedWildcardType {
	
	/**
	 * 计算列表最大元素
	 * @param list
	 * @return
	 */
	public static <T extends Comparable<? super T>> T max(
			List<? extends T> list){
		Iterator<? extends T> i = list.iterator();
		T result = i.next();
		while(i.hasNext()){
			T t = i.next();
			if(t.compareTo(result)>0)
				result = t;
		}
		return result;
	}
	
	public static void swap(List<?> list,int i,int j){
		swapHelper(list,i,j);
	}

	//捕捉通配符类型
	private static <E> void swapHelper(List<E> list, int i, int j) {
		list.set(i,list.set(j, list.get(i)));
	}
}
